package com.example.lib_image

import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.module.AppGlideModule
@GlideModule
class DynamicGlideModule :  AppGlideModule(){
    override fun applyOptions(context: Context, builder: GlideBuilder) {
        // 动态计算内存缓存大小
        val memoryCacheSize = GlideCacheManager.calculateMemoryCacheSize(context)
        builder.setMemoryCache(LruResourceCache(memoryCacheSize))

        // 动态计算磁盘缓存大小和位置
        val (diskCacheSize, diskCacheDir) = GlideCacheManager.getDiskCacheConfig(context) //大小和文件位置
        builder.setDiskCache(InternalCacheDiskCacheFactory(context, diskCacheDir, diskCacheSize))
    }
}